<template>
  <div id="pay_consultation">
    <header>
      <i @click="goBack()"></i>
      <div>用户支付</div>
    </header>

    <div class='content'>
      <div class='input'>
        <span>打赏费用:</span>
        <input type="text" placeholder="请输入大于1的打赏金额" v-model="obj.price">
      </div>
      <label @change="select">
        <div>
          <img src="../../../image/icon_zhifubao_pay.png" width='33' alt="支付宝">
          支付宝支付
        </div>
        <input type="radio" value="支付宝" v-model="payType">
        <i></i>
        <!-- <router-link :to='{name: "authSuccess",query: {id: 1}}'>去支付</router-link> -->
      </label>
      <label @change="select">
        <div>
          <img src="../../../image/icon_weixin_pay.png" width='33' alt="微信">
          微信支付
        </div>
        <input type="radio" value="微信" v-model="payType">
        <i></i>
        <!-- <router-link :to='{name: "authSuccess",query: {id: 1}}'>去支付</router-link> -->
      </label>
      <div class="agbox">
        <label class='agree' @click.stop='toggle(0)' :class="isagree?'checked':''">
          我已阅读并同意相关
          <a @click.stop='toggle(1)'>收费协议</a>
        </label>
      </div>
    </div>
    <div class="footer">
      <a @click='goNext'>付款</a>
    </div>
    <!-- <div class="footer">
            <a @click='refund_alipay'>退款</a>
        </div> -->
    <div class="mc" v-show="showRule" @click.stop='showRule=false'>
      <div class='rules' @click.stop='showRule=true'>
        <h3>收费细则</h3>
        <p>用户需先支付费用等待平台处理</p>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        payType: '支付宝', //
        isagree: false, //是否同意收费细则 默认否
        showRule: false, //是否显示收费细则 默认否
        order_num: '',
        obj: {
          price: ''
        },
      }
    },
    methods: {
      //返回上一级
      goBack() {
        this.$router.back();
      },
      //选择支付类型
      select() {
        console.log(this.payType);
      },
      init() {
        if (this.$route.query.obj) {
          this.obj = JSON.parse(this.$route.query.obj);
          // console.log(this.obj);
        }
      },
      // 退款
      refund_alipay() {
        const data = {};
        if (this.order_num) {
          data.order_num = this.order_num;
        } else {
          data.order_num = new Date().getTime();
        }
        this.$api.post(this.$api.baseURL2, "refund_alipay", data, r => {
          if (r.data.ret == 1) {
            var fund_change = r.data.fund_change;
            if (fund_change == 'Y') {
              this.$utils._toast("退款成功，资金有变动");
            } else if (fund_change == 'N') {
              this.$utils._toast("该订单已经退款成功，资金没有变动！！！");
            }
          } else if (r.data.ret == -2) {
            this.$utils._toast("先支付再退款");
          }
        }, err => {
          console.log(err);
        }, false, false)
      },
      // 支付宝付款
      pay_zhifubao() {
        this.addtoOrder();
      },

      pay_weixin(item) {
        if (this.obj.price < 1) {
          this.$utils._toast("请输入大于1的打赏金额");
          return;
        }
        this.$api.post(this.$api.baseURLSchool, "update_doc_consultation/add", this.obj, r => {
          if (r.data.ret == 1) { //提交订单成功
            this.order_num = r.data.order_num;
            const data = {
              order_num: this.order_num,
              des: '打赏',
              total_fee: this.obj.price,
            };
            this.$api.post(this.$api.baseURL2, "/android_wxpay", data, r => {
              var params = {
                partnerid: r.data.mch_id, // merchant id 
                prepayid: r.data.prepay_id, // prepay id 
                noncestr: r.data.nonce_str, // nonce 
                timestamp: r.data.timestamp, // timestamp 
                sign: r.data.sign, // signed string 
              };
              Wechat.sendPaymentRequest(params, function () {
                this.$utils._toast('支付成功');
              }, function (reason) {
                this.$utils._toast(reason);
              });
            }, err => {
              console.log(err);
            }, false, false)

          }
        }, err => {
          console.log(err);
        })
      },
      //会诊信息添加到会诊表
      addtoOrder() {
        if (this.obj.price < 1) {
          this.$utils._toast("请输入大于1的打赏金额");
          return;
        }
        this.$api.post(this.$api.baseURLSchool, "update_doc_consultation/add", this.obj, r => {
          if (r.data.ret == 1) { //提交订单成功
            this.order_num = r.data.order_num;
            const data = {
              order_num: this.order_num,
              amount: this.obj.price,
              subject: '打赏'
            };
            this.$api.post(this.$api.baseURL2, "/alipay_app", data, r => {
              var params = r.data.data;
              cordova.plugins.alipay.payment(params, function () {
                this.$utils._toast('支付成功');
              }, function (reason) {
                this.$utils._toast(reason);
              });
            }, err => {
              console.log(err);
            }, false, false)

          }
        }, err => {
          console.log(err);
        })
      },
      // 支付
      goNext() {
        if (this.obj.price) {
          var reg = /^[1-9]\d*(\.\d{1,2})?$/g;
          if (this.isagree) {
            if (this.payType == '支付宝') {
              this.pay_zhifubao();
            } else if (this.payType == '微信') {
              this.pay_weixin()
            }
          } else {
            this.$utils._toast("请勾选协议");
          }
        } else {
          this.$utils._toast("请输入打赏金额");
        }

      },
      // 同意或者不同意协议
      toggle(status) { //0指同意或者不同意 1指点的是收费协议
        if (status) {
          this.showRule = true;
        } else {
          if (this.isagree) {
            this.isagree = false;
          } else {
            this.isagree = true;
          }
        }
      },
    },
    created() {
      // 获取支付订单号
      // this.$api.get(this.$api.baseURL,"ali_order",{},r => {
      //     // console.log(r);
      //     this.order_num = r.data.info;
      // },err => {
      //     console.log(err);
      // },false,false)
      this.init();
    }
  }

</script>
<style lang="scss" scoped>

</style>
